package com.springapp.redis.dao.impl;

import com.springapp.model.User;
import com.springapp.redis.dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;

/**
 * Created by guoqing on 2016/3/24 0024.
 */
public class UserDAOImpl implements UserDAO {

    @Autowired
    protected RedisTemplate redisTemplate;

    public void saveUser(final User user) {
//        redisTemplate.execute(new RedisCallback<Object>() {
//            public Object doInRedis(RedisConnection connection) throws DataAccessException {
//                connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getId()),
//                        redisTemplate.getStringSerializer().serialize(user.getName()));
//                return null;
//            }
//        });
    }

    public User getUser(final long id) {
//        return redisTemplate.execute(new RedisCallback<User>() {
//            public User doInRedis(RedisConnection connection) throws DataAccessException {
//                byte[] key = redisTemplate.getStringSerializer().serialize("user.uid." + id);
//                if (connection.exists(key)) {
//                    byte[] value = connection.get(key);
//                    String name = redisTemplate.getStringSerializer().deserialize(value);
//                    User user = new User();
//                    user.setName(name);
//                    user.setId(id);
//                    return user;
//                }
//                return null;
//            }
//        });
        return null;
    }
}
